package cloud.demarcia.rabbitregister.Handler;

import cloud.demarcia.rabbitregister.exception.RabbitForgetException;
import cloud.demarcia.rabbitregister.exception.RabbitLoginException;
import cloud.demarcia.rabbitregister.exception.RabbitRegisterException;
import cloud.demarcia.rabbitregister.exception.RabbitUtilException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author Bojun Wen, wen_bj@outlook.com
 * @date 2022.02.18
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(RabbitRegisterException.class)
    @ResponseBody
    public ResponseEntity<String> handleRegisterException(RabbitRegisterException e){
        return ResponseEntity.status(e.getErrorCode()).body(e.getErrorMessage());
    }
    @ExceptionHandler(RabbitLoginException.class)
    @ResponseBody
    public ResponseEntity<String> handleLoginException(RabbitLoginException e){
        return ResponseEntity.status(e.getErrorCode()).body(e.getErrorMessage());
    }
    @ExceptionHandler(RabbitForgetException.class)
    @ResponseBody
    public ResponseEntity<String> handleForgetException(RabbitForgetException e){
        return ResponseEntity.status(e.getErrorCode()).body(e.getErrorMessage());
    }
    @ExceptionHandler(RabbitUtilException.class)
    @ResponseBody
    public ResponseEntity<String> handleForgetException(RabbitUtilException e){
        return ResponseEntity.status(e.getErrorCode()).body(e.getErrorMessage());
    }
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseEntity<String> handleUnknownException(Exception e){
        log.error(e.getMessage());
        return ResponseEntity.status(498).body("服务器错误，请稍后再试");
    }

}
